/*
 * TCSS 360 - Autumn 2011
 * Group2: ManuscriptPR 
 * 
 * ConfMembershipCollection class
 * Author: Tony Butterfield
 */
package user;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * a class containing a collection of conference/role relationhips and methods to manage them.
 * @author Tony Butterfield
 * @version 11/29/2011
 */
public class ConfMembershipCollection 
{
  // INSTANCE FIELDS **************************************
  /**
   * a collection that maps Role values to Conference keys.
   */
  private Map<Conference, Role> my_memberships;


  // CONSTRUCTORS *****************************************
  /**
   * creates a new instance of ConfMembershipCollection with an empty collection.
   */
  public ConfMembershipCollection()
  {
    my_memberships = new HashMap<Conference, Role>();
  }


  // ACCESSORS ********************************************
  /**
   * get reference to a conference with a given name in the collection.
   * @param the_conference_name String to search for
   * @return Conference that matches the name, or null
   ********************************************************
   * @requires the_conference_name = String
   * @ensures returns Conference with the_conference_name, or null if there is no match
   */  
  public Conference getConference(final String the_conference_name)
  {
    Conference result = null;
        
    for (Conference c : my_memberships.keySet())
    {
      if (c.getConferenceName().equals(the_conference_name))
      {
        result = c;
        break;
      }
    }
    return result;
  }
  
  /**
   * get the Role associated with a given conference.
   * @param the_conference Conference to be queried
   * @return Role value mapped to conference or null
   ********************************************************
   * @requires the_conference = Conference
   * @ensures method returns the Role value mapped to the Conference key or returns null, if 
   *    no match
   */  
  public Role getConferenceRole(final Conference the_conference)
  {
//    System.out.println("getconferencerole: " + my_memberships.get(my_memberships.get(the_conference)));
    return my_memberships.get(the_conference);
  }
  
  /**
   * get the Role associated with a given conference.
   * @param the_conference_name String to be queried
   * @return Role value mapped to conference or null
   ********************************************************
   * @requires the_conference = Conference
   * @ensures method returns the Role value mapped to the Conference key or returns null, if 
   *    no match
   */  
  public Role getConferenceRole(final String the_conference_name)
  {
//    System.out.println(my_memberships.get(getConference(the_conference_name)));
    return my_memberships.get(getConference(the_conference_name));
  }
  
  /**
   * get a sorted list of all conference names in 
   * collection.
   * @return List<String> of sorted names of conferences
   ********************************************************
   * @ensures method returns a sorted List<String> of the names of any conferences in the 
   *  collection
   */
  public List<String> getAllConferenceNames()
  {
    final List<String> conference_name_list = new ArrayList<String>();
    final Set<Conference> conference_set = my_memberships.keySet();

    for (Conference c : conference_set)
    {
      conference_name_list.add(c.getConferenceName());
    }

    Collections.sort(conference_name_list);

    return conference_name_list;
  }

  
  // MUTATORS *********************************************
  /**
   * add a conference/role pair to the membership map collection.
   * @param the_conference Conference to be added as key
   * @param the_role Role to be added as value
   ********************************************************
   * @requires the_conference = Conference object, the_role = Role enumerated value
   * @ensures that:  
   *  -if the Conference is not already a key value in the map, the key/value is added to the 
   *    map.
   *  -if the Conference is already a key value in the map, the Role value of that key is 
   *    updated to match the input value 
   *  (which is all standard behavior for a Map object)               
   */
  public void addMembership(final Conference the_conference, final Role the_role)
  {
    my_memberships.put(the_conference , the_role);
  }


  // METHODS **********************************************
  /**
   * test to see if a conference is the collection.
   * @param the_conference Conference to search for
   * @return boolean TRUE if the_conference is in collection else FALSE
   ********************************************************
   * @requires the_conference = Conference
   * @ensure method returns TRUE if the_conference is a key in the collection. 
   */
  public boolean isMember(final Conference the_conference)
  {
    return my_memberships.containsKey(the_conference);
  }

  /**
   * test to see if a conference with a given name is in the collection.
   * @param the_conference_name String containing name of conference to search for
   * @return boolean TRUE if conference found, else FALSE
   ********************************************************
   * @requires the_conference_name = String
   * @ensures method returns TRUE if conference found, else FALSE
   */
  public boolean isMember(final String the_conference_name)
  {    
    final List<String> name_list = getAllConferenceNames();    
    return name_list.contains(the_conference_name);    
  }
}


